-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat: pluggable JSON schema validator providers #1012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: pluggable JSON schema validator providers #1012
Conversation
|
Elicitation can't work on cloudflare workers at all because it requires a second request from the client to resolve the first request and that's impossible in cloudflare architecture |
Hey, I'm working on this at Cloudflare at the moment. We use a Durable Object to keep the state during the elicitation (and for all stateful MCP interactions). Multiple requests from the client can hit the same Durable Object :) You can read more about it here |
|
@mattzcarey i used the same approach but was still stopped by cloudflare...would you be available for a quick chat some of those days? |
Sure. I'm still very new (I started yesterday) but it's my job to get this working. |
|
@mattzcarey do you have discord or where can I contact you? |
I'm in the cloudflare discord :) |
|
I don't think it is the correct move to fully switch over to I am pushing for the library to move away from these hard-coded dependencies (Ajv, Zod, etc) to a flexible core with the ability to inject them, allowing developers to choose what is best for their use case. I created a fork @enth/mcp-sdk that allows you to use the any JSON schema validator (including pre-compiled ones) or schema definition library. Would love to hear your thoughts on it and if that approach would work for your use case. |
|
Hey @jake-danton awesome work on the fork. I ran some benchmarks on this change with some simple examples. I can imagine with deeply nested schemas this gap would widen. Slower sure, but I'm not sure this constitutes a completely new dev experience. There are other efficiencies that are lower hanging fruit in the SDK nevermind the fact that an LLM is always going to be the slowest part of this system. There is ongoing chat about this internally, we could support AJV in its current form but the validator would have to be declared in the global scope for the I'll check out your repo and benchmarks more tomorrow, it is definitely cool work. |
|
I believe this might resolve: #857 |
|
I think the right approach here is to make the validator configurable and less of a hard-dependency as @jake-danton suggests. I am wary about moving to different validator given perf implications and since ajv seems to be the de-facto standard in the ecosystem. We must also consider that any validator must be able to support 2020-12 as a schema, since we are moving to this as the default schema. |
Thanks @dsp-ant will make a pr for this direction. |
5dc06e3 to
05f23aa
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @mattzcarey - thanks for working on this!
I believe there's an issue with removing ajv from dependencies though, since the SDK still needs it available for the default validator. (Adding it to devDependencies makes it available when working on the SDK itself, but e.g. apps that depend on the SDK won't bring it in.)
IIUC restoring the hard dependency should be fine w.r.t. the original problem, since the ajv's compile() will still never run when a custom validator is passed in.
fwiw here's what I did to confirm the problem:
-
Build and pack the SDK:
npm install
npm run build
npm pack # Creates modelcontextprotocol-sdk-1.20.1.tgz -
Create test dir and install sdk (doesn't bring in peer or dev deps):
mkdir /tmp/mcp-test
cd /tmp/mcp-test
npm init -y
npm install /path/to/modelcontextprotocol-sdk-1.20.1.tgz -
try to import:
echo "const { Client } = require('@modelcontextprotocol/sdk/client')" > test.js
node test.js
node:internal/modules/cjs/loader:1413
throw err;
^
Error: Cannot find module 'ajv'
Require stack:
- /private/tmp/mcp-test/node_modules/@modelcontextprotocol/sdk/dist/cjs/validation/ajv-provider.js
- /private/tmp/mcp-test/node_modules/@modelcontextprotocol/sdk/dist/cjs/client/index.js
- /private/tmp/mcp-test/test.js
...
6dfea9f to
ca77e8b
Compare
ca77e8b to
1e75e04
Compare
|
Great spot thanks @bhosmer-ant I added |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! LGTM
Fixes #689 by adding the concept of pluggable JSON schema validators.
Supports ajv and cfworker (works well on edge runtimes).
Also upgrades to ajv v8
Motivation and Context
AJV consistently causes issues in Workers due to its use of
eval. You can see a repro of this using the latest v8 version and other related bugs hereHow Has This Been Tested?
This is tested against a non exhaustive set of json schema 2020-12 spec (in prep for SEP-1330) and the limited set of features supported here
Breaking Changes
This is not breaking and should not be noticed by users unless it unblocks them on edge runtimes.
Users can opt in by declaring a
jsonSchemaValidatorin bothClientandServerTypes of changes
Checklist
Additional context